import { _decorator, dragonBones, JsonAsset } from "cc";
import { app } from "db://app-game/core/app";
import AnimatorDragonBones from "db://app-game/libs/animator/AnimatorDragonBones";
import { AnimatorStateLogic } from "db://app-game/libs/animator/core/AnimatorStateLogic";
import { Role } from "../Role";
import { RoleUpperBodyAnimatorEnum } from "../Role.type";
import { AnimationEventHandler } from "./animator/AnimationEventHandler";
import { RoleUpperStateIdle } from "./animator/upper-body/RoleUpperStateIdle";
import { RoleUpperStateSkills } from "./animator/upper-body/RoleUpperStateSkills";

const { ccclass, property, requireComponent, disallowMultiple } = _decorator;
/**
 * 角色SPINE动画控制
 *
 * 实现功能
 * 1、控制动作变化
 * 2、控制武器变化
 * 3、控制脸的朝向
 */
@ccclass("RoleViewUpperBodyAnimator")
@disallowMultiple
@requireComponent(dragonBones.ArmatureDisplay)
export class RoleViewUpperBodyAnimator extends AnimatorDragonBones {
  /** 角色对象 */
  role: Role = null!;

  protected async start(): Promise<void> {
    // 1.获取状态机
    const animatorGraphPath =
      this.role.RoleModel.data.anim.upperBody.animatorGraphPath;
    const config = await app.res.loadAsync(animatorGraphPath, JsonAsset);
    this.AssetRawUrl = config;

    super.start();

    // 动画状态机
    let anim = new AnimationEventHandler<dragonBones.EventObject>(
      this.playAnimation.bind(this)
    );
    let asl: Map<string, AnimatorStateLogic> = new Map();
    asl.set(
      RoleUpperBodyAnimatorEnum.Idle,
      new RoleUpperStateIdle(this.role, anim)
    );

    asl.set(
      RoleUpperBodyAnimatorEnum.Skill,
      new RoleUpperStateSkills(this.role, anim)
    );

    this.initArgs(asl, anim);
  }

  // /**
  //  * 播放动画
  //  * @override
  //  * @param animName 动画名
  //  * @param loop 是否循环播放
  //  */
  // protected playAnimation(animName: string, loop: boolean) {
  //   if (!animName) {
  //     return;
  //   }

  //   this._animation.play(animName);
  //   this._animState = this._animation.getState(animName);
  //   if (!this._animState) {
  //     return;
  //   }
  //   if (!this._wrapModeMap.has(this._animState)) {
  //     this._wrapModeMap.set(this._animState, this._animState.wrapMode);
  //   }
  //   this._animState.wrapMode = loop
  //     ? 2
  //     : this._wrapModeMap.get(this._animState)!;
  // }

  // /**
  //  * 缩放动画播放速率
  //  * @override
  //  * @param scale 缩放倍率
  //  */
  // protected scaleTime(scale: number) {
  //   if (this._animState) {
  //     this._animState.speed = scale;
  //   }
  // }
}
